﻿using MovingChameLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Geolocation;

namespace MovingChame.WindowsStoreApp.Helpers
{
    public static class GeolocationHelper
    {
        public static async Task<Geoposition> GetCurrentLocation()
        {
            Geolocator geolocator = new Geolocator();
            if (geolocator.LocationStatus == PositionStatus.Disabled || geolocator.LocationStatus == PositionStatus.NoData ||
                geolocator.LocationStatus == PositionStatus.NotAvailable)
                return null;

            CancellationTokenSource cancelToken = new CancellationTokenSource();
            var token = cancelToken.Token;

            try
            {
                geolocator.DesiredAccuracy = PositionAccuracy.High;
                var position = await geolocator.GetGeopositionAsync().AsTask(token);
                return position;
            }
            catch (Exception ex)
            {
                return null;
            }
            //return await geolocator.GetGeopositionAsync(TimeSpan.FromDays(Constants.GeoLocation_MaximumAge), TimeSpan.FromHours(Constants.GeoLocation_Timeout));
        }
    }
}